<?php
/**
 * Created by PhpStorm.
 * User: tandunzhao
 * Date: 2017/11/20
 * Time: 下午5:26
 */

namespace App\Console\Commands;

use App\Modules\Channel\Models\ChannelDailyStat;
use App\Modules\SendOrder\Models\SendOrder;
use DB;
use Illuminate\Console\Command;
use Log;

class UpdateCostCommand extends Command
{
    /**
     * 执行命令   php artisan force_user_active
     *
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'update_cost';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '更新日报中的成本';

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $start_date = date('Y-m-d', strtotime('-365 day'));
        $end_date = date('Y-m-d', strtotime('-1 day'));
        $this->updateCost($start_date, $end_date);
    }

    function updateCost($start_date, $end_date)
    {
        $table_name = 'promotion_groups_channel_day_stats';
        $data = ChannelDailyStat::getListData($start_date, $end_date);
        foreach ($data as $item) {
            $id = $item->id;
            $end_time = $item->date . ' 23:59:59';
            $begin_time = $item->date . ' 00:00:00';
            $distribution_channel_id = $item->distribution_channel_id;
            $cost = SendOrder::getDuringCost($distribution_channel_id, $begin_time, $end_time);
            $updated_at = date("Y-m-d H:i:s");
            DB::table($table_name)->where('id', $id)->update(compact('cost', 'updated_at'));
        }
    }
}